/**
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */
/*!
 * \file ts_fused_add_topk_div_tc.cpp
 * \brief FusedAddTopkDiv用例.
 */

#include "ts_fused_add_topk_div.h"

FusedAddTopkDivCase InitNormalCase(int64_t groupNum, int64_t groupTopk, int64_t n, int64_t k, int64_t activateType,
                                   bool isNorm, bool enableExpertMapping, float scale, ge::DataType xDtype, int64_t a,
                                   int64_t b, int64_t c, ge::graphStatus result, int64_t tilingKey)
{
    FusedAddTopkDivCase cs;

    cs.mParam = {groupNum, groupTopk, n, k, activateType, isNorm, enableExpertMapping, scale, xDtype, a, b, c};
    if (result == ge::GRAPH_SUCCESS) {
        cs.mOpInfo.mExp.mTilingKey = tilingKey;
        cs.mOpInfo.mCtr.mRunTiling = true;
        cs.mOpInfo.mCtr.mRunKernel = true;
    } else {
        cs.mOpInfo.mExp.mSuccess = false;
        cs.mOpInfo.mCtr.mRunTiling = true;
        cs.mOpInfo.mCtr.mRunKernel = false;
    }
    cs.Init();
    return cs;
}

void InitAndRunNormalCase(int64_t groupNum, int64_t groupTopk, int64_t n, int64_t k, int64_t activateType,
                          bool isNorm, bool enableExpertMapping, float scale, ge::DataType xDtype, int64_t a,
                          int64_t b, int64_t c, ge::graphStatus result, int64_t tilingKey)
{
    FusedAddTopkDivCase cs = InitNormalCase(groupNum, groupTopk, n, k, activateType, isNorm, enableExpertMapping, scale,
                                            xDtype, a, b, c, result, tilingKey);
    if (result == ge::GRAPH_SUCCESS) {
        ASSERT_TRUE(cs.Run());
    } else {
        ASSERT_FALSE(cs.Run());
    }
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_tiling_key_0_success)
{
    InitAndRunNormalCase(8, 4, 2, 8, 0, true, false, float(2.5), ge::DT_FLOAT, 256, 256, 99,
                         ge::GRAPH_SUCCESS, 0);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_tiling_key_1_success)
{
    InitAndRunNormalCase(8, 4, 2, 8, 0, true, false, float(2.5), ge::DT_FLOAT16, 256, 256, 99,
                         ge::GRAPH_SUCCESS, 1);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_tiling_key_2_success)
{
    InitAndRunNormalCase(8, 4, 2, 8, 0, true, false, float(2.5), ge::DT_BF16, 256, 256, 99,
                         ge::GRAPH_SUCCESS, 2);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_tiling_key_10_success)
{
    InitAndRunNormalCase(8, 4, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT, 256, 256, 99,
                         ge::GRAPH_SUCCESS, 10);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_tiling_key_11_success)
{
    InitAndRunNormalCase(8, 4, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 256, 256, 99,
                         ge::GRAPH_SUCCESS, 11);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_tiling_key_12_success)
{
    InitAndRunNormalCase(8, 4, 2, 8, 0, true, true, float(2.5), ge::DT_BF16, 256, 256, 99,
                         ge::GRAPH_SUCCESS, 12);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_tiling_key_11_one_batch_per_core_success)
{
    InitAndRunNormalCase(8, 4, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                         ge::GRAPH_SUCCESS, 11);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_x_dim_1_failed)
{
    FusedAddTopkDivCase cs = InitNormalCase(8, 4, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                                            ge::GRAPH_FAILED, 11);
    cs.x = Tensor("x", {cs.mParam.a}, "1", cs.mParam.xDtype, ge::FORMAT_ND);
    ASSERT_FALSE(cs.Run());
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_add_num_dim_2_failed)
{
    FusedAddTopkDivCase cs = InitNormalCase(8, 4, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                                            ge::GRAPH_FAILED, 11);
    cs.addNum = Tensor("addNum", {cs.mParam.b, 1}, "2", cs.mParam.xDtype, ge::FORMAT_ND);
    ASSERT_FALSE(cs.Run());
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_x_add_num_shape_failed)
{
    FusedAddTopkDivCase cs = InitNormalCase(8, 4, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                                            ge::GRAPH_FAILED, 11);
    cs.addNum = Tensor("addNum", {1}, "1", cs.mParam.xDtype, ge::FORMAT_ND);
    ASSERT_FALSE(cs.Run());
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_mapping_num_dim_2_failed)
{
    FusedAddTopkDivCase cs = InitNormalCase(8, 4, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                                            ge::GRAPH_FAILED, 11);
    cs.mappingNumOptional = Tensor("mappingNum", {cs.mParam.b, 1}, "2", ge::DT_INT32, ge::FORMAT_ND);
    ASSERT_FALSE(cs.Run());
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_x_mapping_num_shape_failed)
{
    FusedAddTopkDivCase cs = InitNormalCase(8, 4, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                                            ge::GRAPH_FAILED, 11);
    cs.mappingNumOptional = Tensor("mappingNum", {1}, "1", ge::DT_INT32, ge::FORMAT_ND);
    ASSERT_FALSE(cs.Run());
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_mapping_table_dim_1_failed)
{
    FusedAddTopkDivCase cs = InitNormalCase(8, 4, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                                            ge::GRAPH_FAILED, 11);
    cs.mappingTableOptional = Tensor("mappingTable", {cs.mParam.b}, "1", ge::DT_INT32, ge::FORMAT_ND);
    ASSERT_FALSE(cs.Run());
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_x_mapping_table_shape_failed)
{
    FusedAddTopkDivCase cs = InitNormalCase(8, 4, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                                            ge::GRAPH_FAILED, 11);
    cs.mappingTableOptional = Tensor("mappingTable", {1, cs.mParam.c}, "2", ge::DT_INT32, ge::FORMAT_ND);
    ASSERT_FALSE(cs.Run());
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_mapping_table_dim_two_over_128_failed)
{
    InitAndRunNormalCase(8, 4, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 199,
                         ge::GRAPH_FAILED, 11);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_group_num_0_failed)
{
    InitAndRunNormalCase(0, 4, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                         ge::GRAPH_FAILED, 11);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_group_topk_0_failed)
{
    InitAndRunNormalCase(8, 0, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                         ge::GRAPH_FAILED, 11);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_n_0_failed)
{
    InitAndRunNormalCase(8, 4, 0, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                         ge::GRAPH_FAILED, 11);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_k_0_failed)
{
    InitAndRunNormalCase(8, 4, 2, 0, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                         ge::GRAPH_FAILED, 11);
}


TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_b_not_div_group_num_failed)
{
    InitAndRunNormalCase(8, 4, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 31, 99,
                         ge::GRAPH_FAILED, 11);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_group_topk_gt_group_num_failed)
{
    InitAndRunNormalCase(8, 14, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                         ge::GRAPH_FAILED, 11);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_k_gt_b_failed)
{
    InitAndRunNormalCase(8, 4, 2, 288, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                         ge::GRAPH_FAILED, 11);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_n_gt_group_eles_failed)
{
    InitAndRunNormalCase(8, 4, 72, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                         ge::GRAPH_FAILED, 11);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_group_eles_gt_32_failed)
{
    InitAndRunNormalCase(4, 4, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                         ge::GRAPH_FAILED, 11);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_activate_type_not_0_failed)
{
    InitAndRunNormalCase(8, 4, 2, 8, 1, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                         ge::GRAPH_FAILED, 11);
}

TEST_F(Ts_FusedAddTopkDiv, fused_add_topk_div_b_ge_32_group_num_not_8_failed)
{
    InitAndRunNormalCase(32, 4, 2, 8, 0, true, true, float(2.5), ge::DT_FLOAT16, 16, 256, 99,
                         ge::GRAPH_FAILED, 11);
}
